package view;

import javax.swing.*;
import java.awt.*;

import data.*;

public class BlockView extends JPanel implements ViewForBlock {
    JLabel blockNameOrIcon; //用来显示 Block 对象的 name、number 和 mineIcon 属性
    ImageIcon icon; // 图片
    ImageIcon coverIcon; // 覆盖图片
    JButton blockCover;     //用来遮挡 blockNameOrIcon.
    CardLayout card;        //卡片式布局
    Block block;           //被提供视图的方块

    BlockView() {
        card = new CardLayout();
        setLayout(card);
        blockNameOrIcon = new JLabel("", JLabel.CENTER);
        blockNameOrIcon.setHorizontalTextPosition(AbstractButton.CENTER);
        blockNameOrIcon.setVerticalTextPosition(AbstractButton.CENTER);
        blockCover = new JButton();
        icon = null;
        add("cover", blockCover);
        add("view", blockNameOrIcon);
    }

    public void acceptBlock(Block block) {
        this.block = block;
    }

    public void setCoverIcon(ImageIcon ic) {
        coverIcon = ic;
        blockCover.setIcon(coverIcon);
    }

    public void setDataOnView() {
        icon = block.getMineicon();
        blockNameOrIcon.setIcon(icon);
//        if (block.isMine()) {
//            blockNameOrIcon.setText(block.getName());
//            blockNameOrIcon.setIcon(block.getMineicon());
//        } else {
//            int n = block.getAroundMineNumber();
//            if (n >= 1)
//                blockNameOrIcon.setText("" + n);
//            else
//                blockNameOrIcon.setText(" ");
//        }
    }

    public void seeBlockNameOrIcon() {
        // 图片自动收缩
        setDataOnView();
        Image img = icon.getImage();
        int newWidth = blockNameOrIcon.getWidth();  // 获取 JLabel 的宽度
        int newHeight = blockNameOrIcon.getHeight();  // 获取 JLabel 的高度
        Image scaledImg = img.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH);
        blockNameOrIcon.setIcon(new ImageIcon(scaledImg));
        card.show(this, "view");
        validate();
    }

    public void seeBlockCover() {
        Image img = coverIcon.getImage();
        int newWidth = blockCover.getWidth();  // 获取 JLabel 的宽度
        int newHeight = blockCover.getHeight();  // 获取 JLabel 的高度
        if (newHeight == 0 || newWidth == 0) {
            newWidth = 50;
            newHeight = 50;
        }
        Image scaledImg = img.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH);
        blockCover.setIcon(new ImageIcon(scaledImg));
        card.show(this, "cover");
        validate();
    }

    public JButton getBlockCover() {
        return blockCover;
    }

}
